package aplicacion;

import clases.Fecha;
import clases.Hora;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import acercade.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Aplicacion.java
 *
 * Created on 24-04-2011, 04:15:24 PM
 */

/**
 *
 * @author Administrador
 */
public class Aplicacion extends javax.swing.JFrame {

    /** Creates new form Aplicacion */
    public Aplicacion() {
        initComponents();
        setfecha(true);
        fechaActual.setText(Fecha.getFecha(true, false));
        HiloHora h = new HiloHora(this);
        h.start();
        initAcercaDe();
        this.setLocationRelativeTo(null);
        res.setDoubleBuffered(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        mesFecNac = new javax.swing.JComboBox();
        diaFecNac = new javax.swing.JComboBox();
        añoFecNac = new javax.swing.JComboBox();
        mesFecAct = new javax.swing.JComboBox();
        añoFecAct = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        meses = new javax.swing.JCheckBox();
        segundos = new javax.swing.JCheckBox();
        dias = new javax.swing.JCheckBox();
        minutos = new javax.swing.JCheckBox();
        todas = new javax.swing.JCheckBox();
        horas = new javax.swing.JCheckBox();
        años = new javax.swing.JCheckBox();
        diaFecAct = new javax.swing.JComboBox();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        fechaActual = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        horaActual = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        fecNac = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        res = new javax.swing.JEditorPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuItemAcercaDe = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Necesarios"));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        mesFecNac.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" }));
        mesFecNac.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mesFecNacActionPerformed(evt);
            }
        });
        jPanel1.add(mesFecNac, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 30, -1, -1));

        diaFecNac.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        diaFecNac.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                diaFecNacActionPerformed(evt);
            }
        });
        jPanel1.add(diaFecNac, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 30, -1, -1));

        añoFecNac.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1920", "1921", "1922", "1923", "1924", "1925", "1926", "1927", "1928", "1929", "1930", "1931", "1932", "1933", "1934", "1935", "1936", "1937", "1938", "1939", "1940", "1941", "1942", "1943", "1944", "1945", "1946", "1947", "1948", "1949", "1950", "1951", "1952", "1953", "1954", "1955", "1956", "1957", "1958", "1959", "1960", "1961", "1962", "1963", "1964", "1965", "1966", "1967", "1968", "1969", "1970", "1971", "1972", "1973", "1974", "1975", "1976", "1977", "1978", "1979", "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011" }));
        añoFecNac.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                añoFecNacActionPerformed(evt);
            }
        });
        jPanel1.add(añoFecNac, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 30, -1, -1));

        mesFecAct.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" }));
        mesFecAct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mesFecActActionPerformed(evt);
            }
        });
        jPanel1.add(mesFecAct, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 60, -1, -1));

        añoFecAct.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1920", "1921", "1922", "1923", "1924", "1925", "1926", "1927", "1928", "1929", "1930", "1931", "1932", "1933", "1934", "1935", "1936", "1937", "1938", "1939", "1940", "1941", "1942", "1943", "1944", "1945", "1946", "1947", "1948", "1949", "1950", "1951", "1952", "1953", "1954", "1955", "1956", "1957", "1958", "1959", "1960", "1961", "1962", "1963", "1964", "1965", "1966", "1967", "1968", "1969", "1970", "1971", "1972", "1973", "1974", "1975", "1976", "1977", "1978", "1979", "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011" }));
        añoFecAct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                añoFecActActionPerformed(evt);
            }
        });
        jPanel1.add(añoFecAct, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 60, -1, -1));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Formato de Salida"));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel7.setText("¿En que formato quiere saber la cantidad de tiempo de su existencia?");
        jPanel2.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 20, -1, -1));

        meses.setText("Meses");
        jPanel2.add(meses, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 100, -1, -1));

        segundos.setText("Segundos");
        jPanel2.add(segundos, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 100, -1, -1));

        dias.setText("Dias");
        jPanel2.add(dias, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 70, -1, -1));

        minutos.setText("Minutos");
        jPanel2.add(minutos, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 70, -1, -1));

        todas.setText("Todas");
        todas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                todasActionPerformed(evt);
            }
        });
        jPanel2.add(todas, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 40, -1, -1));

        horas.setText("Horas");
        jPanel2.add(horas, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 100, -1, -1));

        años.setText("Años");
        jPanel2.add(años, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 70, -1, -1));

        jPanel1.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, 410, 130));

        diaFecAct.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        diaFecAct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                diaFecActActionPerformed(evt);
            }
        });
        jPanel1.add(diaFecAct, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 60, -1, -1));

        jButton2.setText("Poner Fecha del Sistema");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 90, 190, -1));

        jLabel4.setText("Fecha Final:");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 60, -1, 20));

        jLabel5.setText("Fecha de Nacimiento:");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 30, -1, 20));

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos a Procesar"));
        jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Fecha Final:");
        jPanel4.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 40, -1, 20));

        fechaActual.setFont(new java.awt.Font("Tahoma", 0, 12));
        fechaActual.setText("xx/xx/xxxx");
        jPanel4.add(fechaActual, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 40, 70, 20));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Hora Actual: ");
        jPanel4.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 60, -1, 20));

        horaActual.setFont(new java.awt.Font("Tahoma", 0, 12));
        horaActual.setText("xx:xx");
        jPanel4.add(horaActual, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 60, 100, 20));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Fecha de Nacimiento:");
        jPanel4.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 20, -1, 20));

        fecNac.setFont(new java.awt.Font("Tahoma", 0, 12));
        fecNac.setText("xx-xx-xxxx");
        jPanel4.add(fecNac, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 20, 70, 20));

        jPanel1.add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 250, 410, 100));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 430, 360));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultados"));
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        res.setContentType("text/html");
        jScrollPane1.setViewportView(res);

        jPanel3.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 320, 330));

        getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 0, 340, 360));

        jMenu1.setText("Ayuda");

        menuItemAcercaDe.setText("Acerca de...");
        jMenu1.add(menuItemAcercaDe);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mesFecNacActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mesFecNacActionPerformed
        calcularDiasDelMes(añoFecNac, mesFecNac, diaFecNac);
        setfecha(true);
    }//GEN-LAST:event_mesFecNacActionPerformed

    private void añoFecNacActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_añoFecNacActionPerformed
        calcularDiasDelMes(añoFecNac, mesFecNac, diaFecNac);
        setfecha(true);
    }//GEN-LAST:event_añoFecNacActionPerformed

    private void añoFecActActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_añoFecActActionPerformed
        calcularDiasDelMes(añoFecAct, mesFecAct, diaFecAct);
        setfecha(false);
    }//GEN-LAST:event_añoFecActActionPerformed

    private void mesFecActActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mesFecActActionPerformed
        calcularDiasDelMes(añoFecAct, mesFecAct, diaFecAct);
        setfecha(false);
    }//GEN-LAST:event_mesFecActActionPerformed

    private void diaFecNacActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_diaFecNacActionPerformed
        setfecha(true);
    }//GEN-LAST:event_diaFecNacActionPerformed

    private void todasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_todasActionPerformed
        if(todas.isSelected()){
            estadoChecksBoxs(true);
        }else{
            estadoChecksBoxs(false);
        }
    }//GEN-LAST:event_todasActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        fechaActual.setText(Fecha.getFecha(true, false));
    }//GEN-LAST:event_jButton2ActionPerformed

    private void diaFecActActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_diaFecActActionPerformed
        setfecha(false);
    }//GEN-LAST:event_diaFecActActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Aplicacion().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox añoFecAct;
    private javax.swing.JComboBox añoFecNac;
    private javax.swing.JCheckBox años;
    private javax.swing.JComboBox diaFecAct;
    private javax.swing.JComboBox diaFecNac;
    private javax.swing.JCheckBox dias;
    private javax.swing.JLabel fecNac;
    private javax.swing.JLabel fechaActual;
    private javax.swing.JLabel horaActual;
    private javax.swing.JCheckBox horas;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenuItem menuItemAcercaDe;
    private javax.swing.JComboBox mesFecAct;
    private javax.swing.JComboBox mesFecNac;
    private javax.swing.JCheckBox meses;
    private javax.swing.JCheckBox minutos;
    private javax.swing.JEditorPane res;
    private javax.swing.JCheckBox segundos;
    private javax.swing.JCheckBox todas;
    // End of variables declaration//GEN-END:variables

    private void calcularDiasDelMes(JComboBox a, JComboBox m, JComboBox d) {
        int mes, año;
        mes = m.getSelectedIndex() + 1;
        año = Integer.parseInt(a.getSelectedItem().toString());

        d.removeAllItems();
        for(int i=1;i<=Fecha.getDiaMayor(mes, año);i++){
            d.addItem(i);
        }
    }

    private void setfecha(boolean isFechaNacimiento) {
        if(isFechaNacimiento){
            fecNac.setText(Fecha.cambiar(diaFecNac.getSelectedIndex()+1) +"-"+Fecha.cambiar(mesFecNac.getSelectedIndex()+1)+"-"+añoFecNac.getSelectedItem().toString());
        }else{
            fechaActual.setText(Fecha.cambiar(diaFecAct.getSelectedIndex()+1) +"-"+Fecha.cambiar(mesFecAct.getSelectedIndex()+1)+"-"+añoFecAct.getSelectedItem().toString());
        }

    }

    private void estadoChecksBoxs(boolean estado) {
        meses.setSelected(estado);
        horas.setSelected(estado);
        dias.setSelected(estado);
        minutos.setSelected(estado);
        segundos.setSelected(estado);
        años.setSelected(estado);
    }

    private void generarResultado() {
        int añoNac = Integer.parseInt(añoFecNac.getSelectedItem().toString());
        int añoAct = Integer.parseInt(fechaActual.getText().substring(6));
        int mesNac = mesFecNac.getSelectedIndex()+1;
        int mesAct = Integer.parseInt(fechaActual.getText().split("-")[1]);

        String texto = "<center><table border = 1 width=100%>";
        texto += "<tr><td colspan = 2><center>TIEMPO DE EXISTENCIA</center></td></tr>";
        int d = Fecha.getDiferenciaEnDias(fecNac.getText(), fechaActual.getText()) - 1 ;
        int a, m;
        a = añoAct - añoNac;

        if(mesNac > mesAct){
            m = 12 - (Math.abs(mesNac-mesAct));
        }else{
            m = Math.abs(mesNac-mesAct);
        }


        if(a != 0){
            if(mesNac > mesAct){
                a = a - 1; //aun no llego al año
            }
             m += a * 12;
        }else{
            m = mesAct - mesNac;
            if (m < 0) { //si es negativo, es porque hay qeu restarlos inverso
                m = m * -1;
            }
        }




        if(años.isSelected()){

            texto += "<tr>";
                texto += "<td>";
                    texto += "Años de Vida";
                texto += "</td>";
                texto += "<td>";
                    texto += a;
                texto += "</td>";
            texto += "</tr>";
        }

        if(meses.isSelected()){
            texto += "<tr>";
                texto += "<td>";
                    texto += "Meses de Vida";
                texto += "</td>";
                texto += "<td>";
                    texto += m;
//                    texto += Integer.toString((((d-1-cantAñoBici)/365)*12)+);
                texto += "</td>";
            texto += "</tr>";
        }

        if(dias.isSelected()){
            texto += "<tr>";
                texto += "<td>";
                    texto += "Dias de Vida";
                texto += "</td>";
                texto += "<td>";
                    texto += Integer.toString(d);
                texto += "</td>";
            texto += "</tr>";
        }

        if(horas.isSelected()){
            texto += "<tr>";
                texto += "<td>";
                    texto += "Horas de Vida";
                texto += "</td>";
                texto += "<td>";
                    texto += Integer.toString(((d)*24)+Integer.parseInt(Hora.getHora()));
                texto += "</td>";
            texto += "</tr>";
        }

        if(minutos.isSelected()){
            texto += "<tr>";
                texto += "<td>";
                    texto += "Minutos de Vida";
                texto += "</td>";
                texto += "<td>";
                    texto += Integer.toString((((d)*24)*60)+(Integer.parseInt(Hora.getHora())*60)+(Integer.parseInt(Hora.getMinuto())));
                texto += "</td>";
            texto += "</tr>";
        }

        if(segundos.isSelected()){
            texto += "<tr>";
                texto += "<td>";
                    texto += "Segundos de Vida";
                texto += "</td>";
                texto += "<td>";
                    texto += Integer.toString(((((d)*24)*60)*60)+(Integer.parseInt(Hora.getHora())*3600)+(Integer.parseInt(Hora.getMinuto())*60)+(Integer.parseInt(Hora.getSegundo())));
                texto += "</td>";
            texto += "</tr>";
        }





        texto += "</table></center>";
        res.setText(texto);
    }
    // End of variables declaration

    private class HiloHora extends Thread{
        private javax.swing.JFrame jf;
        public HiloHora(javax.swing.JFrame jf){
            this.jf = jf;
        }
        @Override
        public void run(){
            while(true){
                horaActual.setText(Hora.getHoraActual());
                jf.setTitle(Hora.getHoraActual()+" JLife - Mide Tu Existencia!");
                generarResultado();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Aplicacion.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    private void initAcercaDe() {
        AcercaDe ad = new AcercaDe("JLife", "Programa para saber cuanto tiempo he vivido hecho en Java. Mide Tu Existencia!", "Patricio Pérez Pinto", "2012", "Patricio Pérez Pinto", GPL.AMBAS_VERSIONES);
        ad.addComponent(this.menuItemAcercaDe);
    }
}
